<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>销量异常处理</title>
        <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
        <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
        <script type="text/javascript" src="/webapp/js/common/config.js"></script>
        <style type="text/css">
            .ruleCheckbox {
                vertical-align: middle;
                zoom: 150%;
            }
        </style>
    </head>
    <body>
        <div class="easyui-container-fluid">
            <div class="easyui-panel" style="border:none;">
                <div class="nav" id="nav"></div>
                <div class="easyui-panel" style="border:none;">
                    <form id="ff">
                        <div style="margin-bottom:20px;">
                     	<span>
                         <label class="item-label">年份：</label>
                         <input type="text" class="easyui-combobox" name="year" id="year" style="width:100px;"/>
                     	</span>
                            <span id="monthLabel">
	                         <label class="item-label">月份：</label>
	                         <input type="text" class="easyui-combobox" name="month" id="month" style="width:100px;"/></span>
                            <span id="weekLabel">
	                         <label class="item-label">周数：</label>
	                         <input type="text" class="easyui-combobox" name="week" id="week" style="width:100px;"/></span>
                            <label class="item-label">品类：</label>
                            <select id="categorySelect" name="categoryCode" class="easyui-combobox" style="width:100px;"></select>
                        </div>
                        <div style="margin-bottom:20px;margin-left: 80px;">
                            <div>
                                <div style="margin-left: 10px;margin-bottom: 10px;margin-top: 10px;">
                                    <div style="font-weight: bold;">设置范围</div>
                                    <div style="margin-left: 40px;margin-bottom: 3px;">
                                        <div style="margin-bottom: 5px;">
                                            <input type="checkbox" name="ruleItems" value="Hc1" class="ruleCheckbox"/>
                                            1.&nbsp;&nbsp;针对
                                            【规格】:
                                            <input type="text" name="propCode_c1" id="propCode_c1" class="easyui-combobox" style="width:100px;" data-options="panelHeight:'auto'"/>
                                            <div style="display: inline-block" name="propValue_c1" id="propValue_c1"></div>
                                            进行调量，其中不参与调量的品牌(用英文逗号分隔)：
                                            <input data-options="width:200,min:0" class="easyui-textbox" type="text" id="hc1_args_1" name="hc1_args_1" style="text-align: right;">
                                        </div>
                                        <div style="margin-bottom: 5px;">
                                            <input type="checkbox" name="ruleItems" value="Hc2" class="ruleCheckbox"/>
                                            2.&nbsp;&nbsp;针对
                                            【规格】:
                                            <input type="text" name="propCode_c2" id="propCode_c2" class="easyui-combobox" style="width:100px;" data-options="panelHeight:'auto'"/>
                                            <div style="display: inline-block" name="propValue_c2" id="propValue_c2"></div>
                                            进行调量，其中不参与调量的品牌(用英文逗号分隔)：
                                            <input data-options="width:200,min:0" class="easyui-textbox" type="text" id="hc2_args_1" name="hc2_args_1" style="text-align: right;">
                                        </div>
                                        <div style="margin-bottom: 5px;">
                                            <input type="checkbox" name="ruleItems" value="Hc3" class="ruleCheckbox"/>
                                            3.&nbsp;&nbsp;针对所有型号不参与调量的品牌(用英文逗号分隔)：<input data-options="width:200,min:0" class="easyui-textbox" type="text" id="hc3_args_1" name="hc3_args_1" style="text-align: right;">
                                        </div>
                                    </div>

                                    <div style="font-weight: bold;">设置规则</div>
                                    <div style="margin-left: 20px;margin-bottom: 3px;">
                                        a.&nbsp;&nbsp;单条记录销量过量
                                    </div>
                                    <div style="margin-left: 40px;margin-bottom: 3px;">
                                        <div style="margin-bottom: 3px;">
                                            <input type="checkbox" name="ruleItems" value="Ha1" class="ruleCheckbox"/>
                                            1.&nbsp;&nbsp;同商场、同型号，若当期量在x区间，且同比环比增幅同时>=y%，若环比同比销量为<input data-options="width:40,min:1" class="easyui-numberbox" type="text" id="ha1_args_1" name="ha1_args_1" style="text-align: right;">台，增幅>=<input data-options="width:50,min:0" class="easyui-numberbox" type="text" id="ha1_args_2" name="ha1_args_2" style="text-align: right;">%，则将此商场此型号取
                                            <input data-options="width:50,min:0" class="easyui-numberbox" type="text" id="ha1_args_3" name="ha1_args_3" style="text-align: right;">%量。<input data-options="width:400,min:0" class="easyui-textbox" type="text" id="ha1_args_4" name="ha1_args_4" style="text-align: right;">
                                        </div>
                                        <div style="margin-bottom: 3px;">
                                            <input type="checkbox" name="ruleItems" value="Ha2" class="ruleCheckbox"/>
                                            2.&nbsp;&nbsp;同商场、同型号，去年无销售，若当期量在x区间，且环比增幅>=y%，若环比销量为<input data-options="width:40,min:1" class="easyui-numberbox" type="text" id="ha2_args_1" name="ha2_args_1" style="text-align: right;">台，增幅>=<input data-options="width:50,min:0" class="easyui-numberbox" type="text" id="ha2_args_2" name="ha2_args_2" style="text-align: right;">%，则将此商场此型号取
                                            <input data-options="width:50,min:0" class="easyui-numberbox" type="text" id="ha2_args_3" name="ha2_args_3" style="text-align: right;">%量。<input data-options="width:400,min:0" class="easyui-textbox" type="text" id="ha2_args_4" name="ha2_args_4" style="text-align: right;">
                                        </div>
                                        <!--<div style="margin-bottom: 3px;">-->
                                        <!--<input type="checkbox" name="ruleItems" value="Ha3" class="ruleCheckbox"/>-->
                                        <!--3.&nbsp;&nbsp;上月销量为<input data-options="width:40,min:1" class="easyui-numberbox" type="text" id="ha3_args_1" name="ha3_args_1" style="text-align: right;">台，增幅>=<input data-options="width:50,min:0" class="easyui-numberbox" type="text" id="ha3_args_2" name="ha3_args_2" style="text-align: right;">%-->
                                        <!--</div>-->
                                        <div style="margin-bottom: 3px;">
                                            <input type="checkbox" name="ruleItems" value="Ha4" class="ruleCheckbox"/>
                                            4.&nbsp;&nbsp;单条记录销量>=<input data-options="width:40,min:0" class="easyui-numberbox" type="text" id="ha4_args_1" name="ha4_args_1" style="text-align: right;">台且量为10的整数倍,禁用
                                        </div>
                                    </div>
                                    <div style="margin-left: 20px;margin-bottom: 3px;">
                                        b.&nbsp;&nbsp;高端型号
                                    </div>
                                    <div style="margin-left: 40px;margin-bottom: 3px;">
                                        <div style="margin-bottom: 5px;">
                                            <input type="checkbox" name="ruleItems" value="Hb1" class="ruleCheckbox"/>
                                            1.&nbsp;&nbsp;价格从高到低排序累计额份额达到<input data-options="width:40,min:0" class="easyui-numberbox" type="text" id="hb1_args_1" name="hb1_args_1" style="text-align: right;">%的型号，销量>=<input data-options="width:40,min:0" class="easyui-numberbox" type="text" id="hb1_args_2" name="hb1_args_2">,禁用
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div style="padding:5px 0;text-align:center;">
                            <div id="progressbar" class="easyui-progressbar" style="width:400px;margin:0 auto;"></div>
                        </div>
                        <div style="padding:5px 0;text-align:center;">
                            <a href="javascript:void(0)" class="easyui-linkbutton c8" onclick="resetAdjust()" style="width:80px;">撤消</a>
                            <a href="javascript:void(0)" class="easyui-linkbutton c8" onclick="runRule()" style="width:80px;margin-left: 20px;" id="runBtn">调整</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <script type="text/javascript">
            var _categoryCode;
            var _year;
            var _month;
            var _week;

            $(function () {
                setNav();
                initUserCategorySelect("categorySelect",showRule);
                $.when(initMonthWeek("monthLabel", "weekLabel")).done(showRule);
                aRecords("销量异常处理New","/webapp/pages/product/audit/salescounthandle")
            });
            //回显规则
            function showRule() {
                $.when(
                    initModelSearchSelect("propCode_c1", "propValue_c1", $("#categorySelect").combobox('getValue'), "propValue_c1"),
                    initModelSearchSelect("propCode_c2", "propValue_c2", $("#categorySelect").combobox('getValue'), "propValue_c2")
                ).done(function () {
                    if (!$('#year').combobox('getValue')) {
                        return;
                    }
                    if (window.parent.workMode == 'week' && !$('#week').combobox('getValue')) {
                        // 周报
                        return;
                    } else if (window.parent.workMode == 'month' && !$('#month').combobox('getValue')) {
                        // 月报
                        return;
                    }
                    if (!$('#categorySelect').combobox('getValue')) {
                        return;
                    }
                    //回显规格
                    var data = getFormData(document.getElementById("ff"));
                    var parm = {};
                    parm.reportType = window.parent.workMode.toUpperCase();
                    parm.categoryCode = data["categoryCode"];
                    parm.year = data["year"];
                    parm.week = data["week"];
                    parm.month = data["month"];
                    _categoryCode = data["categoryCode"];
                    _year = data["year"];
                    _month = data["month"];
                    _week = data["week"];
                    $.ajax({
                        url: config.url + "/salesCountHandle/rule/get",
                        type: "get",
                        data: parm,
                        dataType: 'json',
                        // async: false,
                        xhrFields: {withCredentials: true},
                        beforeSend: function () {
                            $.messager.progress({
                                title: "请稍等",
                                interval: 30,
                                text: "数据加载中..."
                            });
                        },
                        success: function (data) {
                            $.messager.progress("close");
                            if (data.code != 200) {
                                $.messager.alert("提示", data.msg, "warning");
                                return;
                            }
                            var content = data.msg;
                            // if (content && content.categoryCode) {
                            // 填充表单
                            $("#ff").form("load", changeProperty2String(content));
                            if (content.handleType == 1) {
                                // “调整”按钮不可点击
                                $('#runBtn').linkbutton('disable');
                                $('#runBtn').linkbutton({text: '调整中'});

                                $('#progressbar').progressbar('setValue', ~~content.progress);
                                showProgress();
                            } else if (content.handleType == 2) {
                                // “调整”按钮可点击
                                $('#runBtn').linkbutton('enable');
                                $('#runBtn').linkbutton({text: '再次调整'});
                                $('#progressbar').progressbar('setValue', ~~content.progress);
                            } else {
                                // “调整”按钮可点击
                                $('#runBtn').linkbutton('enable');
                                $('#runBtn').linkbutton({text: '调整'});
                                $('#progressbar').progressbar('setValue', ~~content.progress);
                            }
                            // }
                        },
                        error: function () {
                            $.messager.progress("close");
                        }
                    });
                });
            }
            //撤销
            function resetAdjust() {
                if (!$('#categorySelect').combobox('getValue')) {
                    return;
                }
                var formData = {};
                formData.reportType = window.parent.workMode.toUpperCase();
                formData.year = $("#year").combobox("getValue");
                if (isMonthType()) {
                    formData.month = $("#month").combobox("getValue");
                } else {
                    formData.week = $("#week").combobox("getValue");
                }
                formData.categoryCode = $("#categorySelect").combobox("getValue");
                $.messager.confirm("提示", "您确定要撤消之前的调整吗？", function (flag) {
                    if (flag) {
                        $.ajax({
                            url: config.url + "/salesCountHandle/adjust/reset",
                            type: 'POST',
                            data: formData,
                            dataType: "json",
                            xhrFields: {withCredentials: true},
                            beforeSend:function () {
                                $.messager.progress({
                                    title:"请稍等",
                                    interval:30,
                                    text:"数据撤销中..."
                                });
                            },
                            complete:function () {
                                $.messager.progress("close");
                            },
                            success: function (data) {
                                $.messager.progress("close");
                                if (data.code == 200) {
                                    $.messager.alert("提示", data.msg, "info");
                                    showRule();
                                } else {
                                    $.messager.alert("提示", data.msg, "error");
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, thrown) {
                                $.messager.alert("提示", "操作失败", "error");
                            }
                        });
                    }
                });
            }
            //调整
            function runRule() {
                if (!isOfflineType()) {
                    alert("该功能目前只支持线下模式");
                    return;
                }

                var ruleItemsMap = {
                    "Hc1": ["propCode_c1", "propValue_c1", "hc1_args_1"],
                    "Hc2": ["propCode_c2", "propValue_c2", "hc2_args_1"],
                    "Hc3": ["hc3_args_1"],
                    "Ha1": ["ha1_args_1", "ha1_args_2", "ha1_args_3", "ha1_args_4"],
                    "Ha2": ["ha2_args_1", "ha2_args_2", "ha2_args_3", "ha2_args_4"],
                    // "Ha3": ["ha3_args_1", "ha3_args_2"],
                    "Ha4": ["ha4_args_1"],
                    "Hb1": ["hb1_args_1", "hb1_args_2"],
                }

                // 获取已被选中的规则项
                var checkedRuleItems = [];
                $('input[name="ruleItems"]:checked').each(function () {
                    checkedRuleItems.push($(this).val());
                });
                if (checkedRuleItems.length == 0) {
                    $.messager.alert("提示", "请先选择规则项", "warning");
                    return;
                }

                // 验证已选中的规则项设置的参数是否有效
                var formData = getFormData(document.getElementById("ff"));
                var enable = true;
                for (var r = 0; r < checkedRuleItems.length; r++) {
                    var fields = ruleItemsMap[checkedRuleItems[r]];
                    for (var i = 0; i < fields.length; i++) {
                        if (!formData[fields[i]] || formData[fields[i]] <= 0) {
                            enable = false;
                            break;
                        }
                    }
                }

                if (!enable) {
                    $.messager.alert("提示", "所选的规则项参数设置无效", "warning");
                    return;
                }
                formData['reportType'] = window.parent.workMode.toUpperCase();
                $.messager.confirm("提示", "您确定要调整吗？", function (flag) {
                    if (flag) {
                        $("#ff").form("submit", {
                            onSubmit: function () {
                                var valid = $(this).form('enableValidation').form('validate');
                                if (!valid) return false;
                                if (formData['ruleItems'] && typeof formData['ruleItems'] === 'string') {
                                    formData['ruleItems'] = [formData['ruleItems']]
                                }
                                $.ajax({
                                    url: config.url + "/salesCountHandle/rule/run",
                                    type: "POST",
                                    data: JSON.stringify(formData),
                                    dataType: 'json',
                                    contentType: 'application/json;charset=UTF-8',
                                    xhrFields: {withCredentials: true},
                                    success: function (data) {
                                        if (data.code != 200) {
                                            $.messager.alert("提示", data.msg, "error");
                                            return;
                                        }
                                        // “调整”按钮不可点击
                                        $('#runBtn').linkbutton('disable');
                                        $('#runBtn').linkbutton({text: '调整中'});
                                        $('#progressbar').progressbar('setValue', ~~0);
                                        $.messager.show({
                                            title: '提示',
                                            msg: "任务提交成功，正在调整中。。。",
                                            timeout: 1000,
                                            showType: 'slide'
                                        });
                                        showProgress();
                                    },
                                    error: function (XMLHttpRequest, textStatus, thrown) {
                                        // “调整”按钮不可点击
                                        $('#runBtn').linkbutton('disable');
                                        $('#runBtn').linkbutton({text: '调整中'});

                                        // “禁用”按钮不可点击
                                        $('#disableBtn').linkbutton('disable');
                                        $('#disableBtn').linkbutton({text: '禁用'});
                                        $.messager.alert("提示", "任务正在进行中，请勿重复操作", "warning");
                                    }
                                });
                                return false;
                            }
                        });
                    }
                });

            }
            //进度条
            function showProgress() {
                var reportType = window.parent.workMode.toUpperCase();

                $.ajax({
                    url: config.url + "/salesCountHandle/task/progress/get",
                    type: "get",
                    data: {'categoryCode': _categoryCode, 'year': _year, 'month': _month, 'week': _week, 'reportType': reportType},
                    dataType: "json",
                    xhrFields: {withCredentials: true},
                    success: function (data) {
                        if ($('#progressbar').progressbar('getValue') < 100) {
                            $('#progressbar').progressbar('setValue', ~~data.msg);
                            if (data.msg >= 100) {
                                $('#runBtn').linkbutton({text: '完成'});
                                $.messager.show({
                                    title: '提示',
                                    msg: "处理完成",
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else {
                                setTimeout(showProgress, 2000);
                            }
                        }
                    },
                    error: function (data) {
                        console.log(data);
                    }
                });
            }
        </script>
    </body>
</html>